# -*- coding:utf-8 -*-
__author__ = 'Dragon Sun'
__date__ = '2025-07-14 18:35:38'

import pickle
from typing import Union, Optional

import pandas
from dsOKXv5.data_types import 交易方向枚举
from dsOKXv5.models import 持仓模型类
from dsPyLib.rpc.ds_rpc_client import DSRPCClient

from src.conf.conf import G_RPC_HOST, G_RPC_PORT


class RPCClientOKXv5(DSRPCClient):

    def __init__(self, 显示日志: bool = False):
        super().__init__(地址=G_RPC_HOST, 端口=G_RPC_PORT, 显示日志=显示日志)

    def 获取近期K线(self, 数量: int) -> Union[Optional[pandas.DataFrame], Exception]:
        try:
            result = self.m_获取近期K线(数量)
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e

    def 获取当前持仓(self) -> Union[Optional[持仓模型类], Exception]:
        try:
            result = self.m_获取当前持仓()
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e

    def 吃单(
            self, 产品: str, 方向: 交易方向枚举, 数量: float, 杠杆: float, 止盈触发价: float, 止损触发价: float
    ) -> Union[Optional[持仓模型类], Exception]:
        try:
            result = self.m_吃单(产品=产品, 方向=方向, 数量=数量, 杠杆=杠杆, 止盈触发价=止盈触发价, 止损触发价=止损触发价)
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e

    def 反手(self, 止盈触发价: float, 止损触发价: float) -> Union[Optional[持仓模型类], Exception]:
        try:
            result = self.m_反手(止盈触发价=止盈触发价, 止损触发价=止损触发价)
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e

    def 修改止盈触发价(self, 止盈触发价: float) -> Union[Optional[持仓模型类], Exception]:
        try:
            result = self.m_修改止盈触发价(止盈触发价)
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e

    def 修改止损触发价(self, 止损触发价: float) -> Union[Optional[持仓模型类], Exception]:
        try:
            result = self.m_修改止损触发价(止损触发价)
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e

    def 市价全平(self) -> Optional[Exception]:
        try:
            result = self.m_市价全平()
            result = pickle.loads(pickle.dumps(result))  # 将远程对象序列换再反序列化生成本地对象
            return result
        except Exception as e:
            return e
